AI Verified

Name

WooCommerce Payment Gateway Restriction based on Cart Total

About

This code snippet is a WordPress filter function that hooks into WooCommerce's payment gateway system. The primary purpose of this code is to disable the "Cash on Delivery" (COD) payment gateway when the cart total exceeds $200.

Language

PHP

Rating

Voted: 0 by 0 user(s)

How to Setup Snippet

To use the provided code snippet, follow these instructions: 1. Access Your WordPress Theme Files: - Log in to your WordPress admin dashboard. - Navigate to "Appearance" and then "Theme Editor." 2. Locate the `functions.php` File: - In the Theme Editor, find and select the `functions.php` file on the right-hand side. This file is typically located within your currently active theme. 3. Insert the Code Snippet: - Inside the `functions.php` file, scroll down to the end or any empty space. - Copy and paste the entire code snippet at the end of the `functions.php` file. 4. Save Changes: - After pasting the code, click the "Update File" button to save the changes. 5. Test the Functionality: - Go to your WooCommerce store and add products to the cart. - As the cart total approaches or exceeds $200, check the available payment gateways during the checkout process. - The "Cash on Delivery" option should be disabled when the cart total is over $200. Please Note: - Make sure you have a backup of your theme files or your site before making changes, especially if you're not familiar with modifying code. - Always test the functionality in a staging or testing environment before applying it to a live site to ensure it works as expected. - This code assumes that your theme is compatible with the latest version of WooCommerce. If you encounter any issues, consider checking for theme or plugin conflicts. By following these steps, you'll integrate the provided code snippet into your WordPress theme, and it should dynamically disable the "Cash on Delivery" payment option when the cart total exceeds $200 during the checkout process.

Codevault

justin's vault

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: 6.1

Our AI bot has checked this snippet is compatable up to wordpress version: 6.1

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

History

Last modified:

28/11/2023

Important Note

This snippet has the following status:

AI Verified

This snippet has been tested by our AI bot, see any comments below.

AI Bot Comments:

Found 0 vulnerabilities

WooCommerce Payment Gateway Restriction based on Cart Total

 
                    
1add_filter( 'woocommerce_available_payment_gateways', 'disable_cod_over_200' );
2function disable_cod_over_200( $available_gateways ) {
3 // Check if WooCommerce is active
4 if ( ! function_exists( 'WC' ) ) {
5 return $available_gateways;
6 }
7 
8 // Get WooCommerce instance
9 $woocommerce = WC();
10 
11 // Check if cart instance exists
12 if ( is_null( $woocommerce->cart ) ) {
13 return $available_gateways;
14 }
15 
16 // Get cart total
17 $cart_total = $woocommerce->cart->get_cart_contents_total();
18 
19 // Check if cart total is over $200 and disable COD if true
20 if ( $cart_total > 200 ) {
21 unset( $available_gateways['cod'] );
22 }
23 
24 return $available_gateways;
25}

0

Related Snippets

Please see some snippets below related to this snippet..

WooCommerce

AI Verified

2

WooCommerce Automatically Set the Complete Order Status

Added: 1 year ago

Last Updated: 2 months ago

If you’d like to bypass the on-hold order status which either WooCommerce or the payment gateway the customer uses automatically sets on orders, and make it the Completed order status instead, then th...

WooCommerce

AI Verified

0

Remove WooCommerce "Order Again" from Order Received Page

Added: 9 months ago

Last Updated: 9 months ago

WooCommerce

AI Verified

0

Gutenberg for Woocommerce product pages

Added: 2 years ago

Last Updated: 1 year ago

Want to use Gutenberg for Woocommerce? This snippet will make it happen (but might be useless in the future though)

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

General

AI Verified

3

Default featured image

Added: 2 years ago

Last Updated: 2 months ago

This code defines a function named default_post_metadata__thumbnail_id that modifies the value of the _thumbnail_id metadata field for a post. The function takes five arguments: $value: The current...

WordPress Admin

AI Verified

2

Completely Disable Comments

Added: 1 year ago

Last Updated: 6 months ago

This will disable comments on the entire site

WordPress Admin

AI Verified

1

Disable Automatic Updates

Added: 1 year ago

Last Updated: 11 months ago

This will disable automatic updates